package com.zretc.demojparedis.controller;


import com.zretc.demojparedis.commons.PageData;
import com.zretc.demojparedis.commons.RetryOnOptimisticLockingFailure;
import com.zretc.demojparedis.entity.Question;
import com.zretc.demojparedis.entity.Users;
import com.zretc.demojparedis.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("questionapi")
public class QuestionController {

    //创建一个日志记录对象
    Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    private QuestionService questionService;

    //返回值: PageData<Question> :这里方法返回值需要和前端适配【规范】
    //参数:可以按照列举 String keywords / int page /int pagesize
    //参数传递可以是: get ?key=value&key=value

    /**
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("listq")
    @ApiOperation(value = "问题分页查询",notes = "分页")
    public PageData<Question> listQuestion(@RequestParam(name = "keywords", defaultValue = "",required = false) String keywords, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int limit) {

        logger.error("error");
        logger.warn("warn");
        logger.info("info");
        logger.debug("debug");

        //调用service得到Page<Question>对象
        Page<Question> pg = questionService.getQuestionByPage(keywords, page, limit);
        //这里的代码可以再调整一下，也就是说，将如下代码转义到service中
        PageData<Question> pd = new PageData<>();
        pd.setCode(0);
        pd.setData(pg.getContent());
        pd.setCount(pg.getTotalElements());
        pd.setLimit(pg.getSize());
        pd.setMsg("");
        pd.setPage(pg.getNumber() + 1);
        return pd;

    }


    /**
     * 查询单个记录
     * @param qid 问题编号
     * @return 问题对象
     */
    @GetMapping("lsq/{qid}")
    @ApiOperation(value = "单个问题查询",notes = "查询")
    public Question listQuestion2(@ApiParam(name = "qid",value = "问题编号") @PathVariable("qid") int qid) {
        return questionService.getQuestionById(qid);
    }


    /**
     * 乐观锁测试方法1
     * @param qid 问题编号
     * @return  question对象
     */
    @RetryOnOptimisticLockingFailure
    @GetMapping("lsqlock01/{qid}")
    public Question testlock01(@PathVariable int qid) {
        //根据id进行查询
        Question question = questionService.getQuestionById(qid);
        //显示读读取到的version
        logger.info("testlock01 :"+question.getVersion());
        try {
            //模拟执行花了10秒钟
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //尝试修改question
        question.setQtitle("测试testlock01"+Math.random());
        //提交修改操作
        questionService.updateQuestion(question);
        return question;
    }


    @RetryOnOptimisticLockingFailure
    @GetMapping("lsqlock02/{qid}")
    public Question testlock02(@PathVariable int qid) {
        //根据id进行查询
        Question question = questionService.getQuestionById(qid);

        //显示读读取到的version
        logger.info("testlock02 :"+question.getVersion());
        //尝试修改question
        question.setQtitle("测试testlock02"+Math.random());
        //提交修改操作
        questionService.updateQuestion(question);
        return question;
    }


    @GetMapping("testlock03/{qid}")
    public Question testlock03(@PathVariable int qid) {
        //根据id进行查询
        Question question = questionService.getQuestionByIdlock(qid);
        return question;
    }

    @GetMapping("testlock04/{qid}")
    public Question testlock04(@PathVariable int qid) {
        //根据id进行查询
        Question question = questionService.getQuestionByIdlock2(qid);
        return question;
    }






    /**
     * 修改方法
     * @param question
     * @return
     */
    @PutMapping("lsq")

    public Question upadtequestion( @ApiParam(name = "question",value = "问题对象") @RequestBody Question question) {
        return questionService.updateQuestion(question);
    }




    /**
     * 典型的ajax，然后传递数据:json格式 RequestBody
     *
     * @param map
     * @return
     */
    @GetMapping("lsq2")
    public PageData<Question> listQuestion2(@RequestBody Map map) {
        return null;
    }


    //参数传递可以是: get ?key=value&key=value
    @GetMapping("lsq3")
    public PageData<Question> listQuestion3(@RequestParam Map map) {
        return null;
    }
}
